package com.lvandroid.transport.task;

import com.arialyy.aria.core.Aria;
import com.arialyy.aria.core.download.DownloadEntity;
import com.arialyy.aria.core.upload.UploadEntity;

import java.util.ArrayList;
import java.util.List;

/**
 * 用于管理下载/上传任务：
 * 1 已下载的任务
 * 2 正在下载任务
 * 3 下载终止需要恢复的下载任务
 * 4 下载文件MD5属性等
 */
public class TaskManager {

    private static TaskManager sINSTANCE;

    private TaskManager() {
    }

    public static TaskManager getInstance() {
        if(null == sINSTANCE) {
            synchronized (TaskManager.class) {
                if(null == sINSTANCE) {
                    sINSTANCE = new TaskManager();
                }
            }
        }

        return sINSTANCE;
    }

    /**
     * 获取所有已完成的下载任务
     * @return
     */
    public List<TaskInfo> getAllCompleteDownloadTask() {
        List<DownloadEntity> tasks = Aria.download(this).getAllCompleteTask();
        if(null != tasks && tasks.size() > 0) {
            List<TaskInfo> taskInfos = new ArrayList<>();
            for (DownloadEntity entity : tasks
                 ) {
                DownLoadTaskInfo task = new DownLoadTaskInfo(entity.getId(), entity.getRealUrl(), entity.getFilePath());
                task.mTaskComplete = true;
                task.mTotalTaskLength = entity.getFileSize();
                task.mCurrentTaskLength = entity.getFileSize();
                task.mNeedResume = false;
                taskInfos.add(task);
            }

            return taskInfos;
        }

        return null;
    }

    /**
     * 获取所有未完成的下载任务
     * @return
     */
    public List<TaskInfo> getAllNotCompleteDownloadTask() {
        List<DownloadEntity> tasks =  Aria.download(this).getAllNotCompleteTask();
        if(null != tasks && tasks.size() > 0) {
            List<TaskInfo> taskInfos = new ArrayList<>();
            for (DownloadEntity entity : tasks
            ) {
                DownLoadTaskInfo task = new DownLoadTaskInfo(entity.getId(), entity.getRealUrl(), entity.getFilePath());
                task.mTaskComplete = false;
                task.mCurrentTaskLength = entity.getFileSize();
                task.mNeedResume = true;
                taskInfos.add(task);
            }

            return taskInfos;
        }
        return null;
    }

    /**
     * 根据下载任务id获取任务信息
     * @param taskId 下载任务的id
     * @return
     */
    public TaskInfo getDownloadTaskInfo(long taskId) {
        DownloadEntity entity = Aria.download(this).getDownloadEntity(taskId);
        if(null != entity) {
            DownLoadTaskInfo task = new DownLoadTaskInfo(entity.getId(), entity.getRealUrl(), entity.getFilePath());
            task.mTaskComplete = entity.getPercent() == 100;
            if(task.mTaskComplete) {
                task.mTotalTaskLength = entity.getFileSize();
            }
            task.mCurrentTaskLength = entity.getFileSize();
            task.mNeedResume = !task.mTaskComplete;
            return task;
        }

        return null;
    }

    /**
     * 根据下载任务id删除任务
     * @param taskId 任务id
     */
    public void deleteDownloadTask(long taskId) {
        deleteDownloadTask(taskId, false);
    }

    /**
     * 根据下载务id删除任务
     * @param taskId 任务id
     * @param deleteFile 是否删除对应文件
     */
    public void deleteDownloadTask(long taskId, boolean deleteFile) {
        Aria.download(this).load(taskId).cancel(deleteFile);
    }

    /**
     * 获取所有已完成的上传任务
     * @return
     */
    public List<TaskInfo> getAllCompleteUploadTask() {
        List<UploadEntity> tasks = Aria.upload(this).getAllCompleteTask();
        if(null != tasks && tasks.size() > 0) {
            List<TaskInfo> taskInfos = new ArrayList<>();
            for (UploadEntity entity : tasks
            ) {
                UpLoadTaskInfo task = new UpLoadTaskInfo(entity.getId(),
                        entity.isRedirect() ? entity.getRedirectUrl() : entity.getUrl(), entity.getFilePath());
                task.mTaskComplete = true;
                task.mTotalTaskLength = entity.getFileSize();
                task.mCurrentTaskLength = entity.getFileSize();
                task.mNeedResume = false;
                taskInfos.add(task);
            }

            return taskInfos;
        }

        return null;
    }

    /**
     * 获取所有未完成的上传任务
     * @return
     */
    public List<TaskInfo> getAllNotCompleteUploadTask() {
        List<UploadEntity> tasks = Aria.upload(this).getAllNotCompleteTask();
        if(null != tasks && tasks.size() > 0) {
            List<TaskInfo> taskInfos = new ArrayList<>();
            for (UploadEntity entity : tasks
            ) {
                UpLoadTaskInfo task = new UpLoadTaskInfo(entity.getId(),
                        entity.isRedirect() ? entity.getRedirectUrl() : entity.getUrl(), entity.getFilePath());
                task.mTaskComplete = false;
                task.mTotalTaskLength = entity.getFileSize();
                task.mCurrentTaskLength = entity.getFileSize();
                task.mNeedResume = true;
                taskInfos.add(task);
            }

            return taskInfos;
        }

        return null;
    }

    /**
     * 根据上传任务id获取任务信息
     * @param taskId 上传任务的id
     * @return
     */
    public TaskInfo getUploadTaskInfo(long taskId) {
        UploadEntity entity = Aria.upload(this).getUploadEntity(taskId);
        if(null != entity) {
            UpLoadTaskInfo task = new UpLoadTaskInfo(entity.getId(),
                    entity.isRedirect() ? entity.getRedirectUrl() : entity.getUrl(), entity.getFilePath());
            task.mTaskComplete = entity.getPercent() == 100;
            if(task.mTaskComplete) {
                task.mTotalTaskLength = entity.getFileSize();
            }
            task.mCurrentTaskLength = entity.getFileSize();
            task.mNeedResume = !task.mTaskComplete;
            return task;
        }

        return null;
    }

    /**
     * 根据上传任务id删除任务
     * @param taskId 任务id
     */
    public void deleteUploadTask(long taskId) {
        deleteUploadTask(taskId, false);
    }

    /**
     * 根据上传任务id删除任务
     * @param taskId 任务id
     * @param deleteFile 是否删除对应文件
     */
    public void deleteUploadTask(long taskId, boolean deleteFile) {
        Aria.upload(this).load(taskId).cancel(deleteFile);
    }

}
